// OSCClass loop back test sketch
// OSCClass version 1.0.1 (Arduino ver0014)
// Copyright (c) recotana(http://recotana.com).  All right reserved.

#include "Ethernet.h"
#include "OSCClass.h"



  byte serverMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  byte serverIp[]  = { 192, 168, 0, 99 };
  int  serverPort  = 10000;
  
//  byte gateway[]   = { 192, 168, 0, 1 };
//  byte subnet[]    = { 255, 255, 255, 0 };


  byte destIp[]  = { 192, 168, 0, 5};
  int  destPort = 10000;
  
  char *topAddress="/ard";
  char *subAddress[3]={ "/test1" , "/test2" , "/test3" };
  
  OSCMessage recMes;
  OSCMessage sendMes;
  
  OSCClass osc(&recMes);

void setup() {
  
     Ethernet.begin(serverMac ,serverIp);
 //    Ethernet.begin(serverMac ,serverIp ,gateway ,subnet);
    
     //setting osc recieve server
     osc.begin(serverPort);
   
   
     //for message logging
     Serial.begin(19200);
     
     //osc message buffer clear
     osc.flush();
     
     
      sendMes.setIp( destIp );
      sendMes.setPort( destPort );
      sendMes.setTopAddress(topAddress);
}

void loop() {

    //osc arrive check
    if ( osc.available() ) {
      
    logMessage(&recMes);
      
          //toplevel address matching
           if( !strcmp( recMes.getAddress(0) , topAddress ) ){
             
              Serial.println( recMes.getAddress(1) );
              
              //second level address matching           
                if( !strcmp( recMes.getAddress(1) , subAddress[0] ) ){
                       
                      event1();
                
                }
                else if( !strcmp( recMes.getAddress(1) , subAddress[1] ) ){
                                   
                      event2();
                             
                }else if( !strcmp( recMes.getAddress(1) , subAddress[2] ) ){
              
                      event3();
              
                }
          }
  Serial.println("");
    }

}

void event1(){
    Serial.println( " simple loop back !" ); 
    
    recMes.setPort( destPort );
    //sender ip is included in an "recMes" osc message object
    osc.sendOsc( &recMes );
  
  
}

void event2(){
     Serial.println( " data culc loop back !" );
     
     Serial.print(" data[0]=");
   
      long data = recMes.getArgInt(0);  //get recieve data
      Serial.print(data);
      
      data += 10;
      Serial.print(" data[0] + 10 = ");
      Serial.println(data);
      
      recMes.setArgs("i" ,&data);    
      recMes.setPort( destPort );
   
      osc.sendOsc( &recMes );
      
  
}

void event3(){
   Serial.println( " another loop back." );
   
   long    v1 = 123;    //long or unsigned long only (not int)
   double  v2 = 43.23;  //double only ( not float) 
   
    sendMes.setSubAddress(subAddress[2]);
    sendMes.setArgs("if" ,&v1 ,&v2);
  
//  sendMes.setPort( destPort );
//  sendMes.setIp( recMes.getIp() );  //sender ip
  
  osc.sendOsc( &sendMes );
  
}




// *********  utility  ***********************************


void logMessage(OSCMessage *mes){
  
    uint8_t *ip=mes->getIp();
  
     //disp ip & port
    Serial.print("from IP:");
    Serial.print(ip[0],DEC);
    Serial.print(".");
    Serial.print(ip[1],DEC);
    Serial.print(".");
    Serial.print(ip[2],DEC);
    Serial.print(".");
    Serial.print(ip[3],DEC);
    Serial.print(" port:");
    Serial.print(mes->getPort(),DEC);
    Serial.print("   ");
    
    //disp adr
    for(int i = 0 ; i < mes->getAddressNum() ; i++){
      
      Serial.print(mes->getAddress(i));
      
    }
    
    
    //disp type tags
    Serial.print("  ,");
    for(int i = 0 ; i < mes->getArgNum() ; i++){
      
      Serial.print(mes->getTypeTag(i));
      
    }
    Serial.print(" ");
   
   
   //disp args
    for(int i = 0 ; i < mes->getArgNum() ; i++){
      
      switch( mes->getTypeTag(i) ){
        
        case 'i': {
                      
                      Serial.print( mes->getArgInt(i) );
                  }
          break;
        
        case 'f':  {
                      
                      Serial.print( mes->getArgFloat(i) );
                  }
          break;
         
      }
      
       Serial.print(" ");
      
    }
    
    Serial.println(""); 
    
}
